package com.example.demo.service;

import com.example.demo.mapper.domain.Images;
import com.example.demo.mapper.domain.Word;
import com.example.demo.mapper.domain.WordExample;
import com.example.demo.mapper.WordMapper;
import com.example.demo.resp.CommonResp;
import com.example.demo.resp.Wordlist;
import com.example.demo.resp.Wordtext;
import com.example.demo.utils.CopyUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class Wordservice {
    @Autowired
    public WordMapper wordMapper;
    @Resource
    private Imageservice imageservice;

    public CommonResp list()
    {
        CommonResp commonResp = new CommonResp();
        List<Word> words= wordMapper.selectByExample(new WordExample());
        List<Wordlist> wordlists= CopyUtil.copyList(words,Wordlist.class);
        Collections.sort(wordlists, new Comparator<Wordlist>() {
            @Override
            public int compare(Wordlist w1, Wordlist w2) {
                return w2.getReleasetime().compareTo(w1.getReleasetime());
            }
        });
        commonResp.setContent(wordlists);
        return commonResp;
    }
    public CommonResp delet(String id){
        CommonResp commonResp = new CommonResp();
        int insert=wordMapper.deleteByPrimaryKey(id);
        System.out.print(imageservice.delet(id));
        if (insert==0)
        {
            commonResp.setContent(false);
        }else{
            commonResp.setContent(true);
        }
        return commonResp;
    }
    public CommonResp find(String id)
    {
        CommonResp commonResp = new CommonResp();
        WordExample wordExample=new WordExample();
        WordExample.Criteria criteria=wordExample.createCriteria();
        criteria.andArticleidEqualTo(id);
        List<Word> wordlists=wordMapper.selectByExample(wordExample);
//        Wordtext wordtext=CopyUtil.copy(wordlists.get(0),Wordtext.class);
        commonResp.setContent(wordlists);
        return  commonResp;
    }
    public CommonResp add(Word word)
    {
        LocalDateTime now = LocalDateTime.now(); // 获取当前日期和时间
        String year =Integer.toString(now.getYear());
        String month = Integer.toString(now.getMonthValue());
        String day = Integer.toString(now.getDayOfMonth());
        word.setReleasetime(year+"-"+month+"-"+day);

        // 获取当前时间
        String hour = Integer.toString(now.getHour());
        String minute =Integer.toString(now.getMinute()) ;
        String second = Integer.toString(now.getSecond());
        word.setXjtime(hour+":"+minute+":"+second);

        CommonResp commonResp = new CommonResp();
        UUID uuid = UUID.randomUUID();
        String uuidString = uuid.toString();
        word.setArticleid(uuidString);
        word.setTeacherid(uuidString);
        String urlString = word.getImage();
        String urlList = urlString.replace("[", "").replace("]", "").replace("\"", "");
        String[] urlArray = urlList.split(",");
        word.setImage(urlArray[0]);
        for(int i=0;i<urlArray.length;i++)
        {
            Images images=new Images();
            images.setArticleid(uuidString);
            images.setImages(urlArray[i]);
            images.setTime(word.getReleasetime());
            imageservice.add(images);
        }
        int insert=wordMapper.insert(word);
        if (insert==0)
        {
            commonResp.setContent(false);
        }else{
            commonResp.setContent(true);
        }
        return  commonResp;
    }
}
